package xyz.ddlnt.serviceusr.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;
import xyz.ddlnt.commonutil.constant.MqConstants;
import xyz.ddlnt.commonutil.util.RandomUtil;
import xyz.ddlnt.model.dto.PageDTO;
import xyz.ddlnt.model.dto.PostFavRecordDTO;
import xyz.ddlnt.model.dto.UserContentInteractionDTO;
import xyz.ddlnt.model.entity.PostFavRecord;
import xyz.ddlnt.model.vo.PostRecordVO;
import xyz.ddlnt.serviceusr.mapper.PostFavRecordMapper;
import xyz.ddlnt.serviceusr.service.PostFavRecordService;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author ZZULI_SE 210910
 * @data 2025/5/25 16:55
 */
@Service
public class PostFavRecordServiceImpl extends ServiceImpl<PostFavRecordMapper, PostFavRecord> implements PostFavRecordService {
    @Resource
    private PostFavRecordMapper postFavRecordMapper;
    @Resource
    private RabbitTemplate rabbitTemplate;

    @Override
    public Boolean isFavorite(UserContentInteractionDTO userContentInteractionDTO) {
        return postFavRecordMapper.isExits(userContentInteractionDTO);
    }

    @Override
    public Boolean addFav(PostFavRecordDTO postFavRecordDTO) {
        PostFavRecord postFavRecord = PostFavRecord.builder()
                .id(RandomUtil.generate12DigitRandomNumber())
                .postId(postFavRecordDTO.getPostId())
                .userId(postFavRecordDTO.getUserId())
                .favoriteTime(LocalDateTime.now())
                .build();
        if (postFavRecordMapper.insert(postFavRecord) == 1) {
            // 增加当前帖子收藏数
            rabbitTemplate.convertAndSend(MqConstants.USR_POST, MqConstants.COLLECT_COUNT_PLUS_ONE, postFavRecordDTO.getPostId());
            // 添加博主被收藏数
            rabbitTemplate.convertAndSend(MqConstants.USR_USER, MqConstants.COLLECTED_COUNT_PLUS_ONE_QUEUE, postFavRecordDTO.getPostOwnerId());
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Boolean removeFav(UserContentInteractionDTO userContentInteractionDTO) {
        LambdaQueryWrapper<PostFavRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.and(wrapper -> wrapper
                .eq(PostFavRecord::getPostId, userContentInteractionDTO.getId())
                .eq(PostFavRecord::getUserId, userContentInteractionDTO.getUserId()));
        if (postFavRecordMapper.delete(queryWrapper) == 1) {
            // 减少当前帖子收藏数
            rabbitTemplate.convertAndSend(MqConstants.USR_POST, MqConstants.COLLECT_COUNT_MINUS_ONE, userContentInteractionDTO.getId());
            // 删除博主被收藏数
            rabbitTemplate.convertAndSend(MqConstants.USR_USER, MqConstants.COLLECTED_COUNT_MINUS_ONE_QUEUE, userContentInteractionDTO.getBloggerId());
            return true;
        } else {
            return false;
        }
    }

    @Override
    public List<PostRecordVO> getFavList(PageDTO pageDTO) {
        IPage<PostRecordVO> page = new Page<>(pageDTO.getPageNum(), pageDTO.getPageSize());
        return postFavRecordMapper.getFavList(page, pageDTO.getId());
    }
}
